package cn.snow.boot.rabbitmq.controller;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Wanxiang Liu
 * @version 2018/9/15.
 */
@RestController
public class SendController {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @RequestMapping("/api/rabbitmq/send")
    public String send() {
        String content = "Local timestamp: " + System.currentTimeMillis();
        amqpTemplate.convertAndSend("phone", content);
        return content;
    }

    @RequestMapping("/api/rabbitmq/multisend")
    public String multiSend(@RequestParam("count") int count) {
        StringBuilder rst = new StringBuilder();

        for (int i = 0; i < count; i++) {
            long time = System.currentTimeMillis();
            amqpTemplate.convertAndSend("phone", i + " <===> " + time);
            amqpTemplate.convertAndSend("email", i + " <===> " + time);
            rst.append(time);
            rst.append("<br/>");
        }

        return rst.toString();
    }

}
